package Demo.controller;
import Demo.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import Demo.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/test")
    public User selectUserById(){
        User user = userService.selectUserById(1);
        return user;
    }

    @RequestMapping("/aa")
    public String aa(){
        return "A";
    }

    @RequestMapping("/Hi")
    public ModelAndView sayHello() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("Hello");
        modelAndView.addObject("key", 12345);
    //System.out.println("test");
        return modelAndView;
    }
    //如果在使用动态页面时还想跳转到/static/index.html，可以使用重定向return "redirect:/index.html"

    //-----------------------------登录注册功能
    //登录：
    @RequestMapping("/Login")
    public String login(){
        return "Login";
    }

    @GetMapping("/result")
    public ModelAndView result(HttpServletRequest req, HttpServletResponse resp){

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new User();
        user.setName(username);
        user.setPassword(password);

        User u = userService.login(user);

        if(u!=null){
            ModelAndView mv = new ModelAndView();
            if(u.getUid()==1){
                mv.setViewName("Boss");
                mv.addObject("username",u.getName());
                return mv;
            }else if(u.getUid()==0){
                mv.setViewName("Success");
                mv.addObject("username",u.getName());
                return mv;
            }else {
                mv.setViewName("error");
                return mv;
            }
        }else {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("Middle");
            return mv;
        }
    }
    @GetMapping("/middle")
    public String middle(){
        return "Login";
    }
    //注册：
    @GetMapping("/zhuCe")
    public String zhuCe(){
        return "ZhuCe";
    }

    @GetMapping("/end")
    public String end(HttpServletRequest req, HttpServletResponse resp){

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new User();
        user.setName(username);
        user.setPassword(password);
        boolean flag = userService.zhuCe(user);
        if(flag==true){
            return "End1";
        }else{
            return "End2";
        }
    }
}
